﻿namespace StateMachine
{
    public class OrderStateMachine
    {
        // 当前状态
        public OrderState CurrentState { get; private set; }

        // 构造函数，初始化状态为 Created
        public OrderStateMachine()
        {
            CurrentState = OrderState.Created;
        }

        // 处理订单支付
        public void Pay()
        {
            if (CurrentState == OrderState.Created)
            {
                CurrentState = OrderState.Paid;
                Console.WriteLine("订单已支付");
            }
            else
            {
                Console.WriteLine("无法在当前状态下支付订单");
            }
        }

        // 处理订单发货
        public void Ship()
        {
            if (CurrentState == OrderState.Paid)
            {
                CurrentState = OrderState.Shipped;
                Console.WriteLine("订单已发货");
            }
            else
            {
                Console.WriteLine("无法以当前状态发货订单");
            }
        }

        // 处理订单完成
        public void Complete()
        {
            if (CurrentState == OrderState.Shipped)
            {
                CurrentState = OrderState.Completed;
                Console.WriteLine("订单已完成");
            }
            else
            {
                Console.WriteLine("无法在当前状态下完成订单");
            }
        }

        // 处理订单取消
        public void Cancel()
        {
            if (CurrentState == OrderState.Created || CurrentState == OrderState.Paid)
            {
                CurrentState = OrderState.Canceled;
                Console.WriteLine("订单已取消");
            }
            else
            {
                Console.WriteLine("无法在当前状态下取消订单");
            }
        }
    }
}
